var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var TypedocWebpackPlugin = require('typedoc-webpack-plugin');
var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.js');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var utils = require('./utils');
var buildFor = 'demo';
var lib = require('../demo/src/lib.ts');

module.exports = function () {
    return {
        entry: { 'lib': lib },

        resolve: {
            extensions: ['.js', '.json', '.ts']
        },

        module: {
            loaders: [
                //     {
                //     test: /\.js$/,
                //     loader: 'babel-loader',
                //     exclude: /node_modules/,
                //     query: {
                //         presets: ['es2015']
                //     }
                // },
                {
                    test: /\.ts$/,
                    //include: /demo\/src/,
                    exclude: /node_modules/,
                    loaders: [
                        'awesome-typescript-loader',
                        'angular2-template-loader'
                    ],
                },
                {
                    test: /\.html$/,
                    loader: 'html'
                },

                {
                    test: /\.css$/,
                    include: utils.root(buildFor, 'src'),
                    exclude: utils.root(buildFor, 'src', 'assets'),
                    loader: 'raw'
                },

                { test: /\.scss$/, exclude: utils.root(buildFor, 'src', 'assets'), loader: 'raw!sass' },
                { test: /\.scss$/, include: utils.root(buildFor, 'src', 'assets'), loader: ExtractTextPlugin.extract({ fallbackLoader: 'style-loader', loader: 'css!sass' }) },

                { test: /\.(woff|woff2|ttf|eot|svg|map)$/, loader: 'file-loader?name=/assets/[name].[ext]' },
                { test: /\.(png|jpg|gif)$/, loader: 'url-loader?limit=8192&name=/assets/[name].[ext]' }
            ]
        },

        plugins: [
            new webpack.BannerPlugin('Owned by WIT'),

            new webpack.DllPlugin({
                path: utils.root(buildFor, 'src') + '/lib/manifest.json',
                context: __dirname,
                name: 'lib'
            }),

            new webpack.ContextReplacementPlugin(
                /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
                utils.root(buildFor, 'src'),
                {}
            ),


        ],

        output: {
            path: utils.root(buildFor, 'src'),
            filename: './' + buildFor + '/lib/lib.js',
            library: 'lib',
        },
    }
};
